var AE ={};
AE.namespace = function(){
	var a=arguments, o=null, i, j, d;
	for (i=0;i<a.length;i=i+1){
		d=a[i].split(".");
		o=AE;
		for (j=(d[0] == "AE") ? 1 : 0; j<d.length; j=j+1) {
			o[d[j]]=o[d[j]] || {};
			o=o[d[j]];
		}
	}
	return o;
}

var get = function(o){
	var obj = typeof(o)==='string'?document.getElementById(o):o;
	return obj;
}

var getByClass = function(c,n,r){
	var _array = [];
	var _root = r?(typeof(r)==='string'?document.getElementById(r):r):(document)
	var _objs = n&&typeof(n)==='string'&&n.length>0?_root.getElementsByTagName(n):_root.getElementsByTagName('*');

	for(var i=0;i<_objs.length;i++){
		var _cls = _objs[i].className;
		var _cls_array = _cls.split(' ');
		for(var j=0;j<_cls_array.length;j++){
			if(_cls_array[j] == c){
				_array.push(_objs[i]);
			}
		}
	}
	return _array;
}

var addClass = function(o,c){
	if(o.className.indexOf(c)!=-1){
		return;
	}else{
		o.className += ' ' + c;
	}
}

var removeClass = function(o,c){
	if(o.className.indexOf(c)!=-1){
		o.className = o.className.replace(c,'');
	}else{
		return;
	}
}

var loopGetEle = function(o,t){
	if (o.tagName.toUpperCase()!=t) {
		o = o.parentNode;
		return loopGetEle(o,t);
	} else {
		return o;
	}
}

var isChild = function(e,p,t){
	try{
		var _key = false;
		var _element = typeof(e)==='string'?document.getElementById(e):e;
		var _parent = typeof(p)==='string'?document.getElementById(p):p;
		var _objs = t&&typeof(t)==='string'&&t.length>0?_parent.getElementsByTagName(t):_parent.getElementsByTagName('*');

		for(var i=0;i<_objs.length;i++){
			if(_objs[i]==_element){
				_key = true;
				break;
			}
		}
		return _key;
	}catch(e){}
}

/*
		AE.tableView
*/
AE.namespace('AE.tableView');

AE.tableView = {
	defConfig : {
		targetClassName : 'my-table',
		overClassName : 'mouseOver',
		exceptClassName : 'thead'
	},

	handle : {
		over : function(e){
			var e=(e)?e:window.event;
			var el = e.target || e.srcElement;
			var _el = loopGetEle(el,'TR');
			addClass(_el,AE.tableView.defConfig.overClassName);
		},
		out : function(e){
			var e=(e)?e:window.event;
			var el = e.target || e.srcElement;
			var _el = loopGetEle(el,'TR');
			removeClass(_el,AE.tableView.defConfig.overClassName);
		}
	},

	render : function(){
		var els = getByClass(this.defConfig.targetClassName);
		for(var i=0,j=els.length;i<j;i=i+1){
			var trs = els[i].getElementsByTagName('tr');
			for(var o=0,p=trs.length;o<p;o=o+1){
				if(trs[o].className.indexOf(this.defConfig.exceptClassName)==-1){
					trs[o].onmouseover = this.handle.over;
					trs[o].onmouseout = this.handle.out;
				}
			}
		}
	}
}

AE.namespace('AE:clickShow');

AE.clickShow = function(){
	this.defConfig = {};
	var _self = this;

	_self.trigger = [];
	_self.container = [];
	_self.nowShow = undefined;


	_self.show = function(e){
		if (e){
			e.stopPropagation();
		}else{
			window.event.cancelBubble = true;
		}
		var e = e || window.event;
		var el = e.target || e.srcElement;

		var containId = el.id.split('-')[0] + '-content';
		if(_self.nowShow){
			_self.nowShow.style.display = 'none';
		}
		get(containId).style.display = '';
		_self.nowShow = get(containId);
	};

	_self.hide = function(e){
		if (e){
			e.stopPropagation();
		}else{
			window.event.cancelBubble = true;
		}
		var e = e || window.event;
		var el = e.target || e.srcElement;
		var isIn = isChild(el,_self.nowShow);
		if(!isIn && _self.nowShow){
			_self.nowShow.style.display = 'none';
			_self.nowShow = undefined;
		}
	};

	_self.render = function(o,c){
		_self.trigger = getByClass(o);
		_self.container = getByClass(c);
		if(_self.trigger.length !== _self.container.length){
			alert('no match');
			return;
		}
		for(var i=0,j=_self.trigger.length;i<j;i=i+1){
			_self.trigger[i].onclick = _self.show;
		}
		try{
			document.onclick = _self.hide;
		}catch(e){
			alert(e)
		}
	}
}

/*
		AE.tableSort
*/
AE.namespace('AE.tableSortFilter');

AE.tableSortFilter = {
	defConfig : {
		containerClassName : 'thead',
		needRenderClass : 'needAction',
		actionClassName : 'sort-action',
		actionContainerClassName : 'tool-bar clearfix',
		action : {
			'filter': 'Filter by'
		}
	},

	timeout : undefined,

	setup : function(o,id){
			var ctn = document.createElement('div');
			ctn.className = this.defConfig.actionContainerClassName;
			for(key in this.defConfig.action){
				var _link = document.createElement('a');
				_link.className = key;
				_link.id = id + '-trigger'
				_link.className += ' ' + 'AE:triggers';
				_link.innerHTML =  this.defConfig.action[key];
				ctn.appendChild(_link);
			}
		this.container = ctn;
		var els = o.getElementsByTagName('div');
		for(var i=0,j=els.length;i<j;i=i+1){
			if(els[i].className.indexOf('label')!=-1){
				o.insertBefore(ctn,els[i].nextSibling);
			}
		}
		//o.appendChild(ctn);
	},

	handle : {
		over : function(e){
			var e=(e)?e:window.event;
			var el = e.target || e.srcElement;
			var _el = loopGetEle(el,'TH');
			addClass(_el,AE.tableSortFilter.defConfig.actionClassName);
		},
		out : function(e){
			var e=(e)?e:window.event;
			var el = e.target || e.srcElement;
			var _el = loopGetEle(el,'TH');
			removeClass(_el,AE.tableSortFilter.defConfig.actionClassName);
		}
	},

	render : function(){
		var els = getByClass(this.defConfig.containerClassName);
		for(var i=0,j=els.length;i<j;i=i+1){
			var ths = els[i].getElementsByTagName('th');
			for(var o=0,p=ths.length;o<p;o=o+1){
				if(ths[o].className.indexOf(this.defConfig.needRenderClass)!=-1){
					ths[o].onmouseover = this.handle.over;
					ths[o].onmouseout = this.handle.out;
					this.setup(ths[o],ths[o].id);
				}
			}
		}
	}
}

AE.namespace('AE.filterRender');

AE.filterRender = function(){
	var _self = this;
	
	_self.defConfig = {
		contentClass : 'AE:join-content',
		triggerClass : 'AE:join'
	};
	
	_self.setup = function(e){
		var ctn = this.parentNode.parentNode.parentNode.getElementsByTagName('input')[0];
		if(typeof(ctn._value)=='undefined'){
			ctn._value = '';
		}
		if(this.checked){
			ctn._value += this.value + ';';
			ctn.value = ctn._value;
		}else{
			var strings = ctn._value.split(';');
			var tempString = '';
			for(var i=0,j=strings.length;i<j-1;i=i+1){
				if(this.value == strings[i]){
					continue;
				}else{
					tempString += strings[i] + ';';
				}
			}
			ctn._value = tempString;
			ctn.value = ctn._value;
		}
	};
	_self.render = function(){
		var els = getByClass(_self.defConfig.triggerClass);
		for(var i=0,j=els.length;i<j;i=i+1){
			els[i].onclick = _self.setup;
		}
	}
}

AE.namespace('AE.scrollMenu');

AE.scrollMenu = {
	defConfig : {
		menuId : 'scroll-menu',
		scrollClass : 'on-scroll',
		top : 102,
		width : 330,
		margin_top : 20
	},
	container : undefined,
	handle : {
		scroll : function(){
			var st = document.documentElement.scrollTop;
			var wh = AE.scrollMenu.container.offsetWidth;
			if(st >= AE.scrollMenu.defConfig.top){
				addClass(AE.scrollMenu.container,AE.scrollMenu.defConfig.scrollClass);
			}else if(st <= AE.scrollMenu.defConfig.top){
				removeClass( AE.scrollMenu.container,AE.scrollMenu.defConfig.scrollClass);
			}
		}
	},
	render : function(){
		this.container = get(this.defConfig.menuId);
		if(document.all){
			document.documentElement.onscroll = this.handle.scroll;
		}else{
			document.onscroll = this.handle.scroll;
		}
	}
}

AE.namespace('AE.calendar');

AE.calendar = {
	defConfig : {
		renderClass : 'calendar-body',
		renderPlanParam : '_plan',
		renderActualParam : '_actual',
		containerWidth : '1176'
	},

	container : [],

	create : function(s,d,p,c,cls){
		var div = document.createElement('div');
		div.className = cls;
		div.innerHTML = '&nbsp;'
		div.style.left = s + 'px';
		div.style.width = d + 'px';
		div.title = c;
		p.appendChild(div);
	},

	setup : function(o,p,cls){
		var params = o.getAttribute(p).split(';');
		for(var i=0,j=params.length;i<j;i=i+1){
			if (params[i]==""){
				continue;
			}
			var time = params[i].split(',');

			var start_p = time[0].split(':');
			var end_p = time[1].split(':');

			var hours = end_p[0] - start_p[0];
			var minutes = (end_p[1] - start_p[1])/60;
			var seconds = (end_p[2] - start_p[2])/3600;

			var s_p = parseFloat(start_p[0]) + parseFloat(start_p[1]/60) + parseFloat(start_p[2]/3600);

			var s = parseFloat((s_p/24)*this.defConfig.containerWidth);

			var division = parseFloat(hours) + parseFloat(minutes) + parseFloat(seconds);
			var d = parseFloat((division/24)*this.defConfig.containerWidth);

			this.create(s,d,o,[time[0],time[1]],cls);
		}
	},

	render : function(){
		this.container = getByClass(this.defConfig.renderClass);
		for(var i=0,j=this.container.length;i<j;i=i+1){
			this.setup(this.container[i].getElementsByTagName('dd')[0],this.defConfig.renderPlanParam,'zone');
			this.setup(this.container[i].getElementsByTagName('dd')[0],this.defConfig.renderActualParam,'zone-done');
		}
	}
}